<?php

/** Copyright 2014 TinPhp Systems, Inc.
 * @author 2014-10-22
 */
if (!defined('IN_TINPHP'))
{
    exit('Access Denied');
}
require_once('adminApp.php');

/** 理财产品控制类 */
class DealController extends AdminAppController
{

    public function __construct()
    {
        parent::__construct();
        $this->ThisLogic = $this->Logic('DealLogic');
    }
    
    /*********************************  eof 理财项目 借款项目  *********************************/
    
    /** 获取配置选项： */
    public static function getConfSet()
    {        
        // 借款方式
        $type_borrow = self::$conf->confGroup("type_borrow");
        T::$tpl->set("type_borrow", $type_borrow);
        // 放款方式
        $type_repay = self::$conf->confGroup("type_repay");
        T::$tpl->set("type_repay", $type_repay);
        // 借款期限
        $repay_time = self::$conf->confGroup("repay_time");
        T::$tpl->set("repay_time", $repay_time);
        // 收益方式
        $type_loan = self::$conf->confGroup("type_loan");
        T::$tpl->set("type_loan", $type_loan);
        // 融资方
        $finance = self::$conf->confGroup("finance");
        T::$tpl->set("finance", $finance);
        // 借款状态
        $deal_stat = self::$conf->confGroup("deal_stat");
        T::$tpl->set("deal_stat", $deal_stat);
    }
    
    /** 项目列表 */
    public function actionDefault()
    {
        $type = $_REQUEST["t"];
        if($type == "j"){
            $cond = "TYPE=1";
        }else{
            $cond = "TYPE=2";
        }
        T::$tpl->set("type", $type);
        
        $page = intval($_REQUEST["p"]) ? $_REQUEST["p"] : 1;
        $size = 10;
        
        $res = array();
        try
        {
            $res = $this->ThisLogic->getDealList("*", $cond, "order by id desc", $page, $size);
            $par = array("t" => $type);
            $_REQUEST["c"] = $_REQUEST["ctl"];
            $res['page'] = page :: listPage($par, $res["count"], $page, $size);
        } catch (Exception $e)
        {
            echo $e->getMessage();
        }
        T::$tpl->set("res", $res);
        
        // 借款状态
        $deal_stat = self::$conf->confGroup("deal_stat");
        T::$tpl->set("deal_stat", $deal_stat);
        // 获取收益方式
        $type_loan = self::$conf->confGroup("type_loan");
        T::$tpl->set("type_loan", $type_loan);
    }

    /** 添加项目 */
    public function actionAdd()
    {
        T::$tplFile = "admin/deal/act";
        
        if ($_POST['actname'] == 'save')
        {
            $this->SaveData($_POST, 'add');
            return;
        }
        
        self::getConfSet();
    }

    /** 修改项目 */
    public function actionEdit()
    {
        T::$tplFile = "admin/deal/act";
        
        $id = $_REQUEST['id'];
        if ($id)
        {
            if ($_POST['actname'] == 'save')
            {
                $this->SaveData($_POST, 'edit');
            } else
            {
                self::getConfSet();
                
                //显示
                $dealItem = $this->ThisLogic->getDealItem("*", "ID=" . $id);
                T::$tpl->set('dealItem', $dealItem);
            }
        } else
        {
            $this->sysMessage("修改失败,要修改的ID异常");
        }
    }

    /** 保存添加、修改项目 */
    private function SaveData($data, $type)
    {
        if ($type == 'add')
        {
            $res = $this->ThisLogic->addDealItem($data);
            if ($res)
            {
                $this->sysMessage("添加成功");
            } else
            {
                $this->sysMessage("添加失败,请重试");
            }
        } elseif ($type == 'edit')
        {
            $res = $this->ThisLogic->updateDealItem($data, "ID=" . $data['id']);
            if ($res)
            {
                $this->sysMessage("修改成功");
            } else
            {
                $this->sysMessage("修改失败,请重试");
            }
        } else
        {
            $this->sysMessage("未知操作..");
        }
    }

    /** 删除项目 */
    public function actionDel()
    {
       T::$tplFile='public/empty';
        $id = intval($_REQUEST['id']);
        if($id){
            $item = $this->ThisLogic->getDealItem("type, state", "ID=".$id);            
            if((int)$item["TYPE"] == 2){
                $t = "l";
                $title = "理财";
            }else{
                $t = "j";
                $title = "借款";
            }
            
            if($item["STATE"] == 1){
                $this->ThisLogic->delDealItem("ID=".$id);
                return $this->sysMessage("操作成功",array("返回菜单页"=>"admin.php?ctl=deal&t=" . $t), 3);
            }else{
                return $this->sysMessage("删除失败,不能删除该{$title}产品",array("返回菜单页"=>"admin.php?ctl=deal&t=" . $t), 3);
            }            
        }else{
            $this->sysMessage("删除失败,ID异常");
        }
    }

    /*********************************  eof 理财项目  *********************************/
    
    /*********************************  bof 投资记录  *********************************/
    
    /** 投资记录列表 */
    public function actionDealLoad()
    {
        $page = intval($_REQUEST["p"]) ? $_REQUEST["p"] : 1;
        $size = 10;
        $cond = "1=1";
        $pid = intval($_REQUEST["pid"]) ? $_REQUEST["pid"] : 0;
        if(isset($pid) && !empty($pid)){
            $cond .= " and DEAL_ID=" . $pid;
        }
        $res = array();
        
        // 搜索条件可以包含USER_ID, PRICE 融资总额, TITLE 项目名称, MONEY 投资金额, TYPE_LOAN 收益方式, CREATE_TIME
        
        try
        {
            $res = $this->ThisLogic->getDealLoadDealViewList("*, concat('a', USER_ID) as AUSER_ID", $cond, "order by id desc", $page, $size);

            // 获取投资者/投资项目信息
            if(!empty($res["data"])){
                $user_arr = array();
                $deal_arr = array();
                foreach($res["data"] as $k => $v){
                    $user_arr[] = $v["USER_ID"];
                    $deal_arr[] = $v["DEAL_ID"];
                }
                if(!empty($user_arr)){
                    $user_str = implode(",", $user_arr);
                    $user_t = $this->Logic("UserLogic")->getUserInfoList("USERNAME, REALNAME, USERID", "USERID in (" . $user_str . ")");
                    $user_arr = array();
                    foreach($user_t["data"] as $v){
                        $user_arr["A" . $v["USERID"]] = $v;
                    }
                    T::$tpl->set("user_arr", $user_arr);
                }
                // 获取收益方式
                $type_loan = self::$conf->confGroup("type_loan");
                T::$tpl->set("type_loan", $type_loan);
            }

            $par = array(); // 搜索条件变化后，这里也需要修改
            $_REQUEST["c"] = $_REQUEST["ctl"];
            $res['page'] = page :: listPage($par, $res["count"], $page, $size);
        } catch (Exception $e)
        {
            echo $e->getMessage();
        }
        T::$tpl->set("res", $res);
    }

    /** 添加投资记录 */
    public function actionDealLoad_Add()
    {   
        T::$tplFile = "admin/deal/dealload_act";
        
        if ($_POST['actname'] == 'save')
        {
            $this->SaveDealLoadData($_POST, 'add');
            return;
        }
        
        // 过滤不合理条件
        $pid = $_REQUEST["pid"];
        if(!isset($pid) || empty($pid)){
            $this->sysMessage("请重试");
            return;
        }
        
        // 获取选项 收益方式及融资方
        $type_loan = self::$conf->confGroup("type_loan");
        T::$tpl->set("type_loan", $type_loan);
        $finance = self::$conf->confGroup("finance");
        T::$tpl->set("finance", $finance);

        // 获取理财项目
        $dealItem = $this->ThisLogic->getDealItem("*", "ID=" . $pid);
        T::$tpl->set('dealItem', $dealItem);
        
        // 获取已投资总额
        $once_price = $this->ThisLogic->getDealLoadItem("sum(MONEY) as ONCE_PRICE", "DEAL_ID=" . $pid);
        T::$tpl->set("once_price", $dealItem["PRICE"] - $once_price["ONCE_PRICE"]);
    }   

    /** 保存添加、修改投资记录 */
    private function SaveDealLoadData($data, $type)
    {
        if ($type == 'add')
        {
            $res = $this->ThisLogic->addDealLoadItem($data);
            if ($res)
            {
                $this->sysMessage("添加成功");
            } else
            {
                $this->sysMessage("添加失败,请重试");
            }
        } elseif ($type == 'edit')
        {
            $res = $this->ThisLogic->updateDealLoadItem($data, "ID=" . $data['id']);
            if ($res)
            {
                $this->sysMessage("修改成功");
            } else
            {
                $this->sysMessage("修改失败,请重试");
            }
        } else
        {
            $this->sysMessage("未知操作..");
        }
    }
    
    /*********************************  eof 投资记录  *********************************/
}

//End CLass
?>